import { System } from "laya-system";
import Bullet, { Template } from "./Bullet";

export default class NormalBullet extends Bullet {
    target: Laya.Sprite | undefined;
    prefab: Laya.Prefab;
    constructor(prefab: Laya.Prefab) {
        super();
        this.prefab = prefab;
    }
    speed = 15;

    render({ init: { x, y }, angle, speed }: Template): Laya.Sprite {
        this.target = this.prefab.create() as Laya.Sprite;
        System.Bullets.addResource(this.target);
        this.target.pos(x, y);

        const rb = this.target!.getComponent(Laya.RigidBody) as Laya.RigidBody;

        // ! 注意这里添加了一个变量
        (this.target as any).control = this;

        rb.setVelocity({
            x: speed * Math.cos(angle),
            y: speed * Math.sin(angle),
        });
        rb.angularVelocity = this.angleSpeed;

        return this.target;
    }
    destroy() {
        (this.target as any).control = undefined;
        this.target?.destroy();
        this.target = undefined;
    }
}
